<template>
  <a-modal class="changMask"
    title="新增定时任务"
           :footer="null"
    :maskClosable="false"
    :width="1050"
    placement="right"
    @cancel="onClose"
    :visible="jobAddVisiable"
  >
    <a-form :form="form">
      <a-form-item label='Bean名称' v-bind="formItemLayout">
        <a-input style="width: 100%"
                 v-decorator="['beanName',
                   {rules: [
                    { required: true, message: 'Bean名称不能为空'},
                    { max: 50, message: '长度不能超过50个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='方法名称' v-bind="formItemLayout">
        <a-input v-decorator="['methodName',
                   {rules: [
                    { required: true, message: '方法名称不能为空'},
                    { max: 50, message: '长度不能超过50个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='方法参数' v-bind="formItemLayout">
        <a-input v-decorator="['params',
                   {rules: [
                    { max: 50, message: '长度不能超过50个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='Cron表达式'
                   v-bind="formItemLayout"
                   :validateStatus="validateStatus"
                   :help="help">
        <a-cron ref="innerVueCron" v-decorator="['cronExpression', {'initialValue':'0 0 0 1 * ?',rules:
          [{ required: true, message: '请输入cron表达式!' }]}]" @change="setCorn"></a-cron>
      </a-form-item>
      <a-form-item label='备注信息' v-bind="formItemLayout">
        <a-textarea
          :rows="4"
          v-decorator="[
          'remark',
          {rules: [
            { max: 100, message: '长度不能超过100个字符'}
          ]}]">
        </a-textarea>
      </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>

import ACron from '@/components/Acron/ACron'

const formItemLayout = {
  labelCol: {span: 4},
  wrapperCol: {span: 18}
}
export default {
  name: 'JobAdd',
  components: {ACron},
  props: {
    jobAddVisiable: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      job: {
        cronExpression: ''
      },
      validateStatus: '',
      help: ''
    }
  },
  methods: {
    reset () {
      this.loading = false
      this.validateStatus = this.help = ''
      this.job = {cronExpression: ''}
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleSubmit () {
      this.loading = true
      if (this.validateStatus !== 'success') {
        this.checkCron()
      }
      if (this.validateStatus !== 'success') {
        this.loading = false
        return
      }
      this.form.validateFields((err, values) => {
        if (!err) {
          this.setJobFields()
          this.$post('job', {
            ...this.job
          }).then((res) => {
            if (res.data.status) {
              this.reset()
              this.$emit('success')
            } else {
              this.$message.error(res.data.message)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    checkCron () {
      let cron = this.job.cronExpression.trim()
      if (cron.length) {
        this.$get('job/cron/check?cron=' + cron).then((r) => {
          if (!r.data) {
            this.validateStatus = 'error'
            this.help = '请填写合法的Cron表达式'
          } else {
            this.validateStatus = 'success'
            this.help = ''
          }
        })
      } else {
        this.validateStatus = 'error'
        this.help = '请填写Cron表达式'
      }
    },
    setJobFields () {
      let values = this.form.getFieldsValue(['beanName', 'methodName', 'params', 'remark'])
      if (typeof values !== 'undefined') {
        Object.keys(values).forEach(_key => {
          this.job[_key] = values[_key]
        })
      }
    },
    setCorn (data) {
      if (data !== undefined) {
        this.$nextTick(() => {
          this.form.setFieldsValue({cronExpression: data})
          this.job.cronExpression = data
        })
      }
    }
  }
}
</script>
<style scoped>
.ant-form-item{
 margin-bottom: 3rem!important;
}
</style>
